package com.emergency.embackend.common;

import java.util.HashMap;
import java.util.Map;

/**
 * @author chddd
 * @description
 * @create 2024/1/19 9:34:50
 */
public class R extends HashMap<String, Object>   {
    private static final long serialVersionUID = 1L;

    public R() {
        this.put((String)"code", 0);
    }

    public static R error() {
        return error(500, "未知异常，请联系管理员");
    }

    public static R error(String msg) {
        return error(500, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put((String)"code", code);
        r.put((String)"msg", msg);
        return r;
    }

    public static R error(String module, int code, Map map) {
        R r = new R();
        r.put((String)"module", module);
        r.put((String)"code", code);
        r.put((String)"msg", map);
        return r;
    }

    public static R error(String module, Map map) {
        R r = error();
        r.put((String)"module", module);
        r.put((String)"msg", map);
        return r;
    }

    public static R exception(String e) {
        R r = error();
        r.put((String)"exception", e);
        return r;
    }

    public static R exception(String msg, String e) {
        R r = error(msg);
        r.put((String)"exception", e);
        return r;
    }

    public static R exception(String url, String msg, String e) {
        R r = exception(msg, e);
        r.put((String)"url", url);
        return r;
    }

    public static R ok(String msg) {
        R r = new R();
        r.put((String)"msg", msg);
        return r;
    }

    public static R ok(Map<String, Object> map) {
        R r = new R();
        r.putAll(map);
        return r;
    }

    public static R ok() {
        return new R();
    }

    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public boolean isOk() {
        return this.get("code") != null && Integer.parseInt(this.get("code") + "") == 0;
    }

    public boolean isError() {
        return this.get("code") != null && Integer.parseInt(this.get("code") + "") == 500;
    }
}
